﻿Imports System.Reflection
Imports System.Data.SqlClient
Imports System.Collections.Generic

Namespace Utilities
    Public Class DataMapper

        Public Shared Function MapDataToEntity(Of T As {New})(dataReader As SqlDataReader) As IEnumerable(Of T)
            Dim entityType As Type = GetType(T)
            Dim properties = entityType.GetProperties()

            Dim entities As IList(Of T) = New List(Of T)

            If dataReader.HasRows Then
                While dataReader.Read()
                    Dim entity As T = New T

                    For Each info As PropertyInfo In properties
                        Dim columnName As String = info.Name
                        If (dataReader(columnName) IsNot DBNull.Value) Then
                            info.SetValue(entity, dataReader(columnName), Nothing)
                        End If
                    Next

                    entities.Add(entity)
                End While
            End If

            Return entities
        End Function

    End Class
End Namespace

